# Copyright (C) 2006,2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

use strict;
use warnings;

package CCNCore::Model::ServerInterface;

use Jifty::DBI::Schema;

use CCNCore::Record schema {

    # Same for MAC addresses. Note that these are stored as 00:00:00:00:00:00 to avoid
    # automatic conversion to numeric format if only digits are present.

    # -- macaddr is stored as 00:00:00:00:00:00 to avoid issues with DBI / MySQL / SQLite interpreting as a number
    column mac =>
        type is "CHAR(17)",
        is mandatory,
        is unique,
        is immutable,
        # is primary key,
        since '0.0.1',
        label is "MAC Address",
        hints is '';

    column server =>
        references CCNCore::Model::Server,
        is immutable,
        since '0.0.1',
        label is "Server",
        hints is '';

    column rank =>
        type is 'integer',
        is mandatory,
        since '0.0.1',
        label is "Order";


    # -- management
    # -- interface name (eth0, etc.)
    # YYY MySQL does not support TEXT in PRIMARY KEY or UNIQUE
    # XXX @name
    column ifname =>
        type is 'VARCHAR(16)',
        is mandatory,
        since '0.0.1',
        label is "Interface Name (eth0, ..)";

    # These may be NULL if the interface is discovered but not provisioned.
    column ipnet_ip =>
        # MySQL doesn't have a native "inet" type;
        type is 'VARCHAR(16)',
        validator is \&CCN::Framework::Type::IPv4::validator,
        since '0.0.1',
        label is "IP Address";
        
    column ipnet_mask =>
        # MySQL doesn't have a native "inet" type;
        type is 'VARCHAR(16)',
        validator is \&CCN::Framework::Type::IPv4::validator,
        since '0.0.1',
        label is "Network Mask";
    
    column demodhcp =>
        type is 'boolean',
        default_value is 'FALSE',
        since '0.0.1',
        render_as 'Unrendered';
        
    column do_realtime =>
        type is 'boolean',
        valid_values are
            { display => '', value => undef },
            { display => 'Yes', value => 'TRUE' },
            { display => 'No',  value => 'FALSE' },
        since '0.0.1',
        label is "Realtime interface?";

    # by default the system will attempt to provide realtime services on realtime interfaces

    column do_replication =>
        type is 'boolean',
        valid_values are
            { display => '', value => undef },
            { display => 'Yes', value => 'TRUE' },
            { display => 'No',  value => 'FALSE' },
        since '0.0.1',
        label is "Replication interface?";
    # by default the system will attempt to provide replication services on replication interfaces

    # generally speaking, all others services are served on the first interface listed (i.e. rank=1)

    # XXX UNIQUE ("server","rank"),
    # XXX UNIQUE ("server","ifname")
};

use CCNCore::Mixin::Active;
use CCNCore::Mixin::FancyName;

sub since { 0.0.1 }
sub authorize_via { qw( server server ) }
sub menu { 'Servers' }

1;

__END__

=pod
-- #copy WHERE server IN (SELECT pk FROM server WHERE system = $base_system_pk OR system = $system_pk)
CREATE TABLE "server_interface" (
    -- macaddr is stored as 00:00:00:00:00:00 to avoid issues with DBI / MySQL / SQLite interpreting as a number
    "mac"                   macaddr NOT NULL PRIMARY KEY,           -- @mac "MAC Address" create-only

    "server"                INTEGER NOT NULL REFERENCES server(pk), --          "Server" nomodify
    "rank"                  INTEGER NOT NULL,                       -- @digits  "Order"

    -- web-oriented properties
    "label"                 TEXT NOT NULL,                          -- @label   "Interface Label"

    -- management
    -- interface name (eth0, etc.)
    -- YYY MySQL does not support TEXT in PRIMARY KEY or UNIQUE
    "ifname"                VARCHAR(16) NOT NULL,                   -- @name    "Interface Name (eth0, ..)"
    -- These may be NULL if the interface is discovered but not provisioned.
    "ipnet_ip"              inet,                                   -- @ip      "IP Address"
    "ipnet_mask"            inet,                                   -- @ip      "Network Mask"
    "demodhcp"              BOOLEAN DEFAULT 'FALSE',                -- @boolean "hidden"
    "do_realtime"              BOOLEAN,                                -- @boolean "Realtime interface?"
    -- by default the system will attempt to provide realtime services on realtime interfaces
    "do_replication"           BOOLEAN,                                -- @boolean "Replication interface?"
    -- by default the system will attempt to provide replication services on replication interfaces

    -- generally speaking, all others services are served on the first interface listed (i.e. rank=1)

    UNIQUE ("server","rank"),
    UNIQUE ("server","ifname")
);

=cut
